package com.vjwealth.android.nos.activity.rescue_add

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.google.gson.Gson
import com.vjwealth.android.nos.Constants
import com.vjwealth.android.nos.R
import com.vjwealth.android.nos.base.BaseApplication
import com.vjwealth.android.nos.base.BaseLocationActivity
import com.vjwealth.android.nos.entity.MapData
import com.vjwealth.android.nos.entity.RescueCarData
import com.vjwealth.android.nos.entity.RescueCarEntity
import com.vjwealth.android.nos.http.Api
import com.vjwealth.android.nos.http.BaseStringRequest
import com.vjwealth.android.nos.utils.ParamsUtils
import com.vjwealth.android.nos.utils.PhoneUtils
import com.wx.wheelview.widget.WheelViewDialog
import kotlinx.android.synthetic.main.activity_rescue_add.*
import kotlinx.android.synthetic.main.layout_title.*

class RescueAddActivity : BaseLocationActivity(), View.OnClickListener {
    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.mRescueCallBtn -> todoRescueCallBtn()
//            R.id.mRescueCarNoView -> do
        }
    }

    private val accidentTypeHashMap = hashMapOf(
            "0" to "发生轻微事故影响驾驶体验",
            "1" to "发生一般事故造成停驶，但不会导致主要零部件损坏",
            "2" to "发生严重事故造成主要零部件损坏，无法驾驶",
            "3" to "其他")

    private lateinit var mPhoneAddRescuePopWindow: PopupWindow
    private var mCarList: MutableList<MapData>? = null
    private var mRescueCarId = ""
    private var mRescueCarNumber = ""
    private var mRescueTel = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rescue_add)
        initView()
        fetchRescueCarList()
    }
    
    private fun initView(){
        mRescueAddBtn.isEnabled = false
        mTitleNameView.text = "救援服务"
        var name = BaseApplication.spInfor!!.getString("uName")
        mRescueNameView.text = "尊敬的" + name + "先生/女士，请不要着急"
        mRescueAddressView2.setLengthFilter(50)
        mRescueNoteView.setLengthFilter(50)
        initCallPopupWin()
    }

    private fun initCallPopupWin(){
        var popupWinView = mLayoutInflater!!.inflate(R.layout.select_image_popwin, null)
        val cancelTv = popupWinView.findViewById(R.id.cancelBtn) as TextView
        val selectByCameraBtn = popupWinView.findViewById(R.id.selectByCameraBtn) as TextView
        val selectByPhotosBtn = popupWinView.findViewById(R.id.selectByPhotosBtn) as TextView
        selectByCameraBtn.text = resources.getString(R.string.have_add_rescue)
        selectByPhotosBtn.text = resources.getString(R.string.add_rescue)

        mPhoneAddRescuePopWindow = PopupWindow()
        //设置PopupWindow的View
        mPhoneAddRescuePopWindow.setContentView(popupWinView)
        //设置PopupWindow弹出窗体的宽
        mPhoneAddRescuePopWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
        //设置PopupWindow弹出窗体的高
        mPhoneAddRescuePopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
        mPhoneAddRescuePopWindow.setAnimationStyle(R.style.Animation)
        mPhoneAddRescuePopWindow.setFocusable(true)
        mPhoneAddRescuePopWindow.setOutsideTouchable(true)// 设置允许在外点击消失
        //实例化一个ColorDrawable颜色为半透明
        val dw = ColorDrawable(Color.parseColor("#1C1C1C"))
        //设置SelectPicPopupWindow弹出窗体的背景
        mPhoneAddRescuePopWindow.setBackgroundDrawable(dw)
        selectByCameraBtn.setOnClickListener {
            mPhoneAddRescuePopWindow.dismiss()
            this@RescueAddActivity.finish()
        }
        selectByPhotosBtn.setOnClickListener { mPhoneAddRescuePopWindow.dismiss() }
        cancelTv.setOnClickListener { mPhoneAddRescuePopWindow.dismiss() }
    }

    private fun initRescueCarPopupWin(rescueCarDataList: List<RescueCarData>){
        if(rescueCarDataList!=null && rescueCarDataList.isNotEmpty()) {
            mCarList = mutableListOf()
            for (bean in rescueCarDataList) {
                mCarList!!.add(MapData(bean.id, bean.carNumber, bean.insuranceTel))
            }
            mRescueCallBtn.isEnabled = true
            mRescueCarId = mCarList!!.get(0).key.toString()
            mRescueCarNumber = mCarList!!.get(0).value
            mRescueTel = mCarList!!.get(0).extra
            mRescueCarNoView.text = mRescueCarNumber
        }else{
            showExceptionMessage("暂无可添加救援车辆")
        }
    }

    private fun todoRescueCallBtn(){
        if(hasSelectCar()){
            PhoneUtils.intentToCall(this@RescueAddActivity, mRescueTel)
        }else{
            showExceptionMessage("请选择救援车辆")
        }
    }

    private fun todoShowSelectCar(){
        val dialog = WheelViewDialog<MapData>(this@RescueAddActivity)
        dialog.setTitle("请选择使用车辆")
                .setItems(mCarList)
                .setCount(3).show()
        dialog.setmOnDialogBtnClickListener(object : WheelViewDialog.OnDialogBtnClickListener<MapData>{
            override fun onCancelBtnClick() {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onComfirmBtnClick(t: MapData?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        })
    }

    private fun hasSelectCar() : Boolean{
        return !TextUtils.isEmpty(mRescueCarId) &&
                !TextUtils.isEmpty(mRescueCarNumber) &&
                !TextUtils.isEmpty(mRescueTel)
    }

    /*查询可添加救援的车辆*/
    private fun fetchRescueCarList(){
        var params = hashMapOf("customerId" to BaseApplication.spInfor!!.getString(Constants.LOGIN_USER_ID))
        var request = BaseStringRequest(Request.Method.POST, Api.car_rescue_can_list, ParamsUtils.p_map2string(params),
                Response.Listener { response ->
                    var rescueCarEntity = Gson().fromJson(response.toString(), RescueCarEntity::class.java)
                    if(rescueCarEntity.meta.success) {
                        initRescueCarPopupWin(rescueCarEntity.data)
                    }else{
                        showExceptionMessage(rescueCarEntity.meta.message)
                    }
                },
                Response.ErrorListener {  })
    }
}
